package com.baidu.manager.common.utils;

import com.baidu.manager.common.xss.SQLFilter;
import org.apache.commons.lang.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: jql
 * @description: TODO(查询参数)
 * @Date: Created by 15:16 2018/4/13
 */
public class Query extends LinkedHashMap<String, Object> {

    private static final long serialVersionUID = 1L;
    //当前页码
    private int page;
    //每页条数
    private int limit;

    public Query(Map<String, Object> params) {
        this.putAll(params);

        this.page = Integer.parseInt(params.get("page").toString());
        this.limit = Integer.parseInt(params.get("limit").toString());
        this.put("offset", (page - 1) * limit);
        this.put("page", page);
        this.put("limit", limit);

        //防止sql注入（因为sidx、order是通过拼接sql实现排序的，会有sql注入风险）
        String sidex = (String) params.get("sidx");
        String order = (String) params.get("order");

        if (StringUtils.isNotBlank(sidex)) {
            this.put("sidx", SQLFilter.sqlInject(sidex));
        }
        if (StringUtils.isNotBlank(order)) {
            this.put("order", SQLFilter.sqlInject(order));
        }
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }
}
